package com.cn.springcloudwechat.upload;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.controller.base.BaseController;
import com.cn.springcloudwechat.model.attachment.YmAttachment;
import com.cn.springcloudwechat.model.cos.CosClientEntity;
import com.cn.springcloudwechat.model.upload.YmUpload;
import com.cn.springcloudwechat.service.attachment.YmAttachmentService;
import org.apache.commons.collections.map.LinkedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UploadController extends BaseController {
    //COS服务器
    private static final String ATTACHMENT_TYPE_COS="3";
    @Autowired
    YmAttachmentService ymAttachmentService;
    @RequestMapping("uploadVideoAndImage")
    public AjaxRes upload(@RequestParam(value = "videoFile", required = false) MultipartFile videoFile, @RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
                          @RequestParam(value = "imageFile2", required = false) MultipartFile imageFile2,
                          @RequestParam(value = "imageFile3", required = false) MultipartFile imageFile3,
                          @RequestParam(value = "imageFile4", required = false) MultipartFile imageFile4,
                          @RequestParam(value = "imageFile5", required = false) MultipartFile imageFile5,
                          @RequestParam(value = "imageFile6", required = false) MultipartFile imageFile6,
                          @RequestParam(value = "imageFile7", required = false) MultipartFile imageFile7,
                          @RequestParam(value = "imageFile8", required = false) MultipartFile imageFile8,
                          @RequestParam(value = "imageFile9", required = false) MultipartFile imageFile9,
                          @RequestParam(value = "relevanceId", required = false)  Long relevanceId,
                          @RequestParam(value = "relevanceTpyeId", required = false)  String relevanceTpyeId,
                          @RequestParam(value = "videoRelevanceTpyeDetail", required = false) Integer videoRelevanceTpyeDetail,
                          @RequestParam(value = "relevanceTpyeDetail", required = false) Integer imageRelevanceTpyeDetail,
                          @RequestParam(value = "relevanceTpyeDetail2", required = false) Integer imageRelevanceTpyeDetail2,
                          @RequestParam(value = "relevanceTpyeDetail3", required = false) Integer imageRelevanceTpyeDetail3,
                          @RequestParam(value = "relevanceTpyeDetail4", required = false) Integer imageRelevanceTpyeDetail4,
                          @RequestParam(value = "relevanceTpyeDetail5", required = false) Integer imageRelevanceTpyeDetail5,
                          @RequestParam(value = "relevanceTpyeDetail6", required = false) Integer imageRelevanceTpyeDetail6,
                          @RequestParam(value = "relevanceTpyeDetail7", required = false) Integer imageRelevanceTpyeDetail7,
                          @RequestParam(value = "relevanceTpyeDetail8", required = false) Integer imageRelevanceTpyeDetail8,
                          @RequestParam(value = "relevanceTpyeDetail9", required = false) Integer imageRelevanceTpyeDetail9) {
        YmUpload ymUpload = new YmUpload();
        AjaxRes ajaxRes = getAjaxRes();
        try {
            List<YmAttachment> ymAttachments=new ArrayList<YmAttachment>(10);
            String videoFilePath = null;
            String imageFilePath = null;
            String imageFilePath2=null;
            String imageFilePath3 = null;
            String imageFilePath4=null;
            String imageFilePath5= null;
            String imageFilePath6=null;
            String imageFilePath7= null;
            String imageFilePath8=null;
            String imageFilePath9=null;

            if (videoFile != null && videoFile.getOriginalFilename() != null) {
                videoFilePath = FileUtil.upload(videoFile);
            }
            if (imageFile != null && imageFile.getOriginalFilename() != null) {
                imageFilePath = FileUtil.upload(imageFile);
            }
            if (imageFile2 != null && imageFile2.getOriginalFilename() != null) {
                imageFilePath2 = FileUtil.upload(imageFile2);
            }
            if (imageFile3 != null && imageFile3.getOriginalFilename() != null) {
                imageFilePath3 = FileUtil.upload(imageFile3);
            }
            if (imageFile4 != null && imageFile4.getOriginalFilename() != null) {
                imageFilePath4 = FileUtil.upload(imageFile4);
            }
            if (imageFile5 != null && imageFile5.getOriginalFilename() != null) {
                imageFilePath5 = FileUtil.upload(imageFile5);
            }
            if (imageFile6 != null && imageFile6.getOriginalFilename() != null) {
                imageFilePath6= FileUtil.upload(imageFile6);
            }
            if (imageFile7 != null && imageFile7.getOriginalFilename() != null) {
                imageFilePath7 = FileUtil.upload(imageFile7);
            }
            if (imageFile8 != null && imageFile8.getOriginalFilename() != null) {
                imageFilePath8 = FileUtil.upload(imageFile8);
            }
            if (imageFile9 != null && imageFile9.getOriginalFilename() != null) {
                imageFilePath9 = FileUtil.upload(imageFile9);
            }

            if (StringUtils.isNotBlank(videoFilePath)) {
                String filepath=YmUpload.basePath + File.separator + videoFilePath;
                ymUpload.setVideoPath(filepath);
                ymAttachments.add(getYmAttachment(videoFile,filepath,videoRelevanceTpyeDetail));

            }
            if (StringUtils.isNotBlank(imageFilePath)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath;
                ymUpload.setImagePath(filepath);
                ymAttachments.add(getYmAttachment(imageFile,filepath,imageRelevanceTpyeDetail));
            }
            if (StringUtils.isNotBlank(imageFilePath2)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath2;
                ymUpload.setImagePath2(filepath);
                ymAttachments.add(  getYmAttachment(imageFile2,filepath,imageRelevanceTpyeDetail2));
            }

            if (StringUtils.isNotBlank(imageFilePath3)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath3;
                ymUpload.setImagePath3(filepath);
                ymAttachments.add(getYmAttachment(imageFile3,filepath,imageRelevanceTpyeDetail3));
            }

            if (StringUtils.isNotBlank(imageFilePath4)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath4;
                ymUpload.setImagePath4(filepath);
                ymAttachments.add(getYmAttachment(imageFile4,filepath,imageRelevanceTpyeDetail4));
            }

            if (StringUtils.isNotBlank(imageFilePath5)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath5;
                ymUpload.setImagePath5(filepath);
                ymAttachments.add(getYmAttachment(imageFile5,filepath,imageRelevanceTpyeDetail5));
            }

            if (StringUtils.isNotBlank(imageFilePath6)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath6;
                ymUpload.setImagePath6(filepath);
                ymAttachments.add(getYmAttachment(imageFile6,filepath,imageRelevanceTpyeDetail6));
            }

            if (StringUtils.isNotBlank(imageFilePath7)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath7;
                ymUpload.setImagePath7(filepath);
                ymAttachments.add(getYmAttachment(imageFile7,filepath,imageRelevanceTpyeDetail7));
            }

            if (StringUtils.isNotBlank(imageFilePath8)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath8;
                ymUpload.setImagePath8(filepath);
                ymAttachments.add(getYmAttachment(imageFile8,filepath,imageRelevanceTpyeDetail8));
            }

            if (StringUtils.isNotBlank(imageFilePath9)) {
                String filepath=YmUpload.basePath + File.separator + imageFilePath9;
                ymUpload.setImagePath9(filepath);
                ymAttachments.add(getYmAttachment(imageFile9,filepath,imageRelevanceTpyeDetail9));
            }
            ymAttachmentService.insertAttachmentAndRelevance(ymAttachments,relevanceId,relevanceTpyeId);

            for (YmAttachment att:ymAttachments
                 ) {
                System.out.println("ymAttachments"+ att.getAttachmentId());
            }
            System.out.println("relevanceId"+relevanceId);
            System.out.println("relevanceTpyeId"+relevanceTpyeId);
            ajaxRes.setSucceed(ymUpload);
        } catch (IOException e) {
            ajaxRes.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxRes;
    }

    /**
     * 获取附件对象
     * @param file
     * @param filePath
     * @return
     */
    private YmAttachment getYmAttachment(MultipartFile file,String filePath,Integer relevanceTpyeDetail){
        YmAttachment ymAttachment=new YmAttachment();
        ymAttachment.setAttachmentName(file.getOriginalFilename());
        ymAttachment.setAttachmentSuffix(file.getContentType().toString());
        ymAttachment.setAttachmentUrl(filePath);
        ymAttachment.setAttachmentType(ATTACHMENT_TYPE_COS);
        ymAttachment.setRelevanceTpyeDetail(relevanceTpyeDetail);
        return ymAttachment;
    }
}
